/**
 * 
 */
package com.gr.staffpm.tasks.service;

import java.util.Iterator;

import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.model.IModel;

import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.datatypes.User;
import com.gr.staffpm.tasks.model.DetachableTaskModel;


/**
 * @author Graham Rhodes 6 Apr 2011 16:06:43
 */
public class SortableAssignedTaskDataProvider extends SortableDataProvider<Task> {

    private static final long serialVersionUID = 1L;
    private final TaskService taskService;
    private final User user;

    public SortableAssignedTaskDataProvider(TaskService taskService, User user) {
        setSort(Task.DUE_DATE, true);
        this.taskService = taskService;
        this.user = user;
    }

    @Override
    public Iterator<? extends Task> iterator(int first, int count) {
        SortParam sp = getSort();
        return taskService.getOpenUserTasks(user, first, count, sp.getProperty(), sp.isAscending()).iterator();
    }

    @Override
    public IModel<Task> model(Task task) {
        return new DetachableTaskModel(taskService, task);
    }

    @Override
    public int size() {
        return taskService.getOpenUserTasksCount(user);
    }

}
